package org.jtb.quickr;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.jdom.Element;

/**
 * Contacts response.
 *
 * @see org.jtb.quickr.ContactsService#getList(AuthResponse.Token,ContactsGetListCriteria)
 * @see org.jtb.quickr.ContactsService#getPublicList(ContactsGetPublicListCriteria)
 */
public class ContactsResponse extends FlickrResponse {
    /**
     * Contacts contact response.
     */
    public static class Contact extends FlickrResponse {
        private String nsId;
        private String userName;
        private String realName;
        private int iconServer;
        private String iconFarm;
        private Boolean friend;
        private Boolean family;
        private Boolean ignored;
        private URL buddyIconUrl;
        private String staticUrl;
        
        Contact(Element e, String staticUrl) throws QuickrException {
            super(e);
            this.staticUrl = staticUrl;
        }
        
        protected void populate(Element e) {
            this.nsId = e.getAttributeValue("nsid");
            this.userName = e.getAttributeValue("username");
            this.realName = e.getAttributeValue("realname");
            this.iconServer = getAttrInt(e, "iconserver");
            this.iconFarm = e.getAttributeValue("iconfarm");
            this.family = getAttrBoolean(e, "family");
            this.friend = getAttrBoolean(e, "friend");
            this.ignored = getAttrBoolean(e, "ignored");
        }
        
        public String getIconFarm() {
            return iconFarm;
        }
        
        public String getNsId() {
            return nsId;
        }
        
        public String getUserName() {
            return userName;
        }
        
        public String getRealName() {
            return realName;
        }
        
        public int getIconServer() {
            return iconServer;
        }
        
        public Boolean getFriend() {
            return friend;
        }
        
        public Boolean getFamily() {
            return family;
        }
        
        public Boolean getIgnored() {
            return ignored;
        }
        
        public URL getBuddyIconUrl() throws QuickrException {
            return new BuddyIconUrl(staticUrl, iconServer, nsId).getUrl();
        }
    }
    
    
    private static Logger logger = Logger.getLogger(ContactsResponse.class.getName());
    
    private int page;
    private int pages;
    private int perPage;
    private int total;
    private List<Contact> contacts;
    private String staticUrl;
    
    ContactsResponse(InputStream is, String staticUrl) throws QuickrException {
        this.staticUrl = staticUrl;
        init(is);
    }
    
    protected void populate(Element e) throws QuickrException {
        Element contactsElement = e.getChild("contacts");
        
        this.page = getAttrInt(contactsElement, "page");
        this.pages = getAttrInt(contactsElement, "pages");
        this.perPage = getAttrInt(contactsElement, "per_page");
        this.total = getAttrInt(contactsElement, "total");
        
        contacts = new ArrayList<Contact>();
        List contactElements = contactsElement.getChildren("contact");
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("number of contacts: " + contactElements.size());
        }
        for (Iterator i = contactElements.iterator(); i.hasNext(); ) {
            Element contactElement = (Element)i.next();
            Contact c = new Contact(contactElement, staticUrl);
            contacts.add(c);
        }
    }
    
    public static Logger getLogger() {
        return logger;
    }
    
    public int getPage() {
        return page;
    }
    
    public int getPages() {
        return pages;
    }
    
    public int getPerPage() {
        return perPage;
    }
    
    public int getTotal() {
        return total;
    }
    
    public List<Contact> getContacts() {
        return Collections.unmodifiableList(contacts);
    }
    
}
